﻿using Serilog;
using System.Text.Json;
using Worldme.YHChatSDK.Common.Constant;
using Worldme.YHChatSDK.Framework.EventInterface;
using Worldme.YHChatSDK.Framework.Utils;
using Worldme.YHChatSDK.Framework.Vo.V1.Req;
using Worldme.YHChatSDK.Framework.Vo.V1.Ret;
using Worldme.YHChatSDK.Framework.Vo;

namespace YHChatSDKDemo.Plugins
{
    /// <summary>
    /// 普通消息事件插件示例
    /// </summary>
    public class MessageNormalDemo : EventMessageReceiveNormal
    {
        /// <summary>
        /// 相同事件插件中的优先级，越小越优先
        /// </summary>
        public int Order => 0;

        /// <summary>
        /// 返回-1，后面的实现类将不再执行
        /// 返回0，后面的实现类继续执行
        /// </summary>
        /// <param name="eventMsgVo"></param>
        /// <returns></returns>
        public int Handle(EventMsgVo eventMsgVo)
        {
            string chatType = eventMsgVo.@event.message.chatType;
            string chatId = eventMsgVo.@event.message.chatId;
            string senderNickname = eventMsgVo.@event.sender.senderNickname;
            string senderId = eventMsgVo.@event.sender.senderId;
            string contentType = eventMsgVo.@event.message.contentType;
            string msg = null;
            if (ContentTypeConstant.TEXT.Equals(contentType) || ContentTypeConstant.MARKDOWN.Equals(contentType))
            {
                msg = eventMsgVo.@event.message.content.text;
            }
            else if (ContentTypeConstant.IMAGE.Equals(contentType))
            {
                msg = eventMsgVo.@event.message.content.imageUrl + " | " + eventMsgVo.@event.message.content.imageName + " | " + eventMsgVo.@event.message.content.etag;
            }
            else if (ContentTypeConstant.FILE.Equals(contentType))
            {
                msg = eventMsgVo.@event.message.content.fileUrl + " | " + eventMsgVo.@event.message.content.fileName + " | " + eventMsgVo.@event.message.content.fileSize + " | " + eventMsgVo.@event.message.content.etag;
            }
            if (ChatTypeConstant.GROUP.Equals(chatType))
            {
                Log.Information("【群聊普通消息】群号[{chatId}]，用户[{senderId}]（{senderNickname}）发的[{contentType}]消息：{msg}", chatId, senderId, senderNickname, contentType, msg);
                ApiSendMsgReqV1 reqV1 = new ApiSendMsgReqV1()
                        .Text(RecvTypeConstant.GROUP, chatId, "我已经收到了你发的群聊消息：" + msg)
                        .AddButton("跳转", ButtonActionTypeConstant.JUMP_URL, "https://www.baidu.com/", null)
                        .AddButton("复制", ButtonActionTypeConstant.COPY, null, "复制成功啦")
                        .AddButton("上报", ButtonActionTypeConstant.REPORT, null, "上报成功啦");
                ApiSendMsgRetV1 apiSendRetV1 = ApiUtil.SendMsg(reqV1);
                if (apiSendRetV1.code != 1)
                {
                    Log.Error("消息发送失败");
                }
            }
            else if (ChatTypeConstant.BOT.Equals(chatType))
            {
                Log.Information("【私聊普通消息】用户[{senderId}]（{senderNickname}）发的[{contentType}]消息：{msg}", senderId, senderNickname, contentType, msg);
                ApiSendMsgReqV1 reqV1 = new ApiSendMsgReqV1()
                        .Text(RecvTypeConstant.USER, senderId, "我已经收到了你发的私聊消息：" + msg)
                        .AddButton("跳转", ButtonActionTypeConstant.JUMP_URL, "https://www.baidu.com/", null)
                        .AddButton("复制", ButtonActionTypeConstant.COPY, null, "复制成功啦")
                        .AddButton("上报", ButtonActionTypeConstant.REPORT, null, "上报成功啦");
                ApiSendMsgRetV1 apiSendRetV1 = ApiUtil.SendMsg(reqV1);
                Log.Information("请求Json:" + JsonSerializer.Serialize(reqV1));
                Log.Information("结果Json:" + JsonSerializer.Serialize(apiSendRetV1));
                if (apiSendRetV1.code != 1)
                {
                    Log.Error("消息发送失败");
                }
            }
            return 0;
        }
    }
}
